tools: warn_unused_result build fixes.
authorkfraser@localhost.localdomain <kfraser@localhost.localdomain>
Tue, 12 Jun 2007 10:38:31 +0000 (11:38 +0100)
committerkfraser@localhost.localdomain <kfraser@localhost.localdomain>
Tue, 12 Jun 2007 10:38:31 +0000 (11:38 +0100)
Note that some of the existing error checking for asprintf was wrong
for Linux.  asprintf in glibc returns -1 on error, but leaves the
pointer uninitialized.  Only the BSDs zero out the pointer on error.

Also, while fixing these warnings I saw several error paths that were
incorrect.  This patch minimally fixes the warn_unused_result; more
complete error path cleanup will be a later patch.

Signed-off-by: Charles Coffing <ccoffing@novell.com>
tools/blktap/drivers/blktapctrl.c
tools/blktap/drivers/block-qcow.c
tools/blktap/drivers/tapdisk.c
tools/blktap/lib/xenbus.c
tools/blktap/lib/xs_api.c
tools/console/daemon/io.c

index aeec870af29ba20a559e3132fc34158e2b956933..6613768654f8f392d352a9f6a5cf82648cfdff57 100644 (file)
@@ -143,7 +143,8 @@ static int get_new_dev(int *major, int *minor, blkif_t *blkif)
                return -1;
        }
 
-       asprintf(&devname,"%s/%s%d",BLKTAP_DEV_DIR, BLKTAP_DEV_NAME, *minor);
+       if (asprintf(&devname,"%s/%s%d",BLKTAP_DEV_DIR, BLKTAP_DEV_NAME, *minor) == -1)
+               return -1;
        make_blktap_dev(devname,*major,*minor); 
        DPRINTF("Received device id %d and major %d, "
                "sent domid %d and be_id %d\n",
@@ -495,20 +496,27 @@ int blktapctrl_new_blkif(blkif_t *blkif)
 
                if (!exist) {
                        DPRINTF("Process does not exist:\n");
-                       asprintf(&rdctldev, 
-                                "%s/tapctrlread%d", BLKTAP_CTRL_DIR, minor);
-                       blkif->fds[READ] = open_ctrl_socket(rdctldev);
-
+                       if (asprintf(&rdctldev,
+                                    "%s/tapctrlread%d", BLKTAP_CTRL_DIR, minor) == -1)
+                               return -1;
+                       if (asprintf(&wrctldev,
+                                    "%s/tapctrlwrite%d", BLKTAP_CTRL_DIR, minor) == -1) {
+                               free(rdctldev);
+                               return -1;
+                       }
+                       if (asprintf(&cmd, "tapdisk %s %s", wrctldev, rdctldev) == -1) {
+                               free(rdctldev);
+                               free(wrctldev);
+                               return -1;
+                       }
 
-                       asprintf(&wrctldev, 
-                                "%s/tapctrlwrite%d", BLKTAP_CTRL_DIR, minor);
+                       blkif->fds[READ] = open_ctrl_socket(rdctldev);
                        blkif->fds[WRITE] = open_ctrl_socket(wrctldev);
                        
                        if (blkif->fds[READ] == -1 || blkif->fds[WRITE] == -1) 
                                goto fail;
 
                        /*launch the new process*/
-                       asprintf(&cmd, "tapdisk %s %s", wrctldev, rdctldev);
                        DPRINTF("Launching process, CMDLINE [%s]\n",cmd);
                        if (system(cmd) == -1) {
                                DPRINTF("Unable to fork, cmdline: [%s]\n",cmd);
@@ -692,7 +700,8 @@ int main(int argc, char *argv[])
        register_new_unmap_hook(unmap_blktapctrl);
 
        /* Attach to blktap0 */
-       asprintf(&devname,"%s/%s0", BLKTAP_DEV_DIR, BLKTAP_DEV_NAME);
+       if (asprintf(&devname,"%s/%s0", BLKTAP_DEV_DIR, BLKTAP_DEV_NAME) == -1)
+                goto open_failed;
        if ((ret = xc_find_device_number("blktap0")) < 0) {
                DPRINTF("couldn't find device number for 'blktap0'\n");
                goto open_failed;
index a1fab17c55ee4f471d127e4ee20621a5524ce557..928e517ef5baae625c9ee9962e5e3de249008556 100644 (file)
@@ -871,7 +871,10 @@ int tdqcow_open (struct disk_driver *dd, const char *name, td_flag_t flags)
        }
 
        s->fd = fd;
-       asprintf(&s->name,"%s", name);
+       if (asprintf(&s->name,"%s", name) == -1) {
+               close(fd);
+               return -1;
+       }
 
        ASSERT(sizeof(QCowHeader) + sizeof(QCowHeader_ext) < 512);
 
@@ -1165,7 +1168,7 @@ int tdqcow_close(struct disk_driver *dd)
                offset = sizeof(QCowHeader) + sizeof(uint32_t);
                lseek(fd, offset, SEEK_SET);
                out = cpu_to_be32(cksum);
-               write(fd, &out, sizeof(uint32_t));
+               if (write(fd, &out, sizeof(uint32_t))) ;
                close(fd);
        }
 
index 954901b2a8f8eae3395761c718dea90e1d86bca6..94a4e48c30b62a177edfde217a8f38d514a611ff 100644 (file)
@@ -220,7 +220,8 @@ static int map_new_dev(struct td_state *s, int minor)
        fd_list_entry_t *ptr;
        int page_size;
 
-       asprintf(&devname,"%s/%s%d", BLKTAP_DEV_DIR, BLKTAP_DEV_NAME, minor);
+       if (asprintf(&devname,"%s/%s%d", BLKTAP_DEV_DIR, BLKTAP_DEV_NAME, minor) == -1)
+               return -1;
        tap_fd = open(devname, O_RDWR);
        if (tap_fd == -1) 
        {
index fb36e748feed92b049ee495f35570f8df6e12d2f..53a3338f1e9ce27e105bb00a76040fcddc054b81 100644 (file)
@@ -360,8 +360,7 @@ int add_blockdevice_probe_watch(struct xs_handle *h, const char *domid)
        char *path;
        struct xenbus_watch *vbd_watch;
        
-       asprintf(&path, "/local/domain/%s/backend/tap", domid);
-       if (path == NULL) 
+       if (asprintf(&path, "/local/domain/%s/backend/tap", domid) == -1)
                return -ENOMEM;
        
        vbd_watch = (struct xenbus_watch *)malloc(sizeof(struct xenbus_watch));
@@ -399,8 +398,7 @@ int watch_for_domid(struct xs_handle *h)
        struct xenbus_watch *domid_watch;
        char *path = NULL;
 
-       asprintf(&path, "/local/domain");
-       if (path == NULL) 
+       if (asprintf(&path, "/local/domain") == -1)
                return -ENOMEM;
 
        domid_watch = malloc(sizeof(struct xenbus_watch));
index 539d0e3c5175b54d7141ba9660ebdb9f165a97ee..783b4ce3b35e41887720a8bcd2fcaeae5154d116 100644 (file)
@@ -126,10 +126,12 @@ int xs_printf(struct xs_handle *h, const char *dir, const char *node,
        ret = vasprintf(&buf, fmt, ap);
        va_end(ap);
        
-       asprintf(&path, "%s/%s", dir, node);
-       
-       if ((path == NULL) || (buf == NULL))
-               return 0;
+       if (ret == -1)
+               return ENOMEM;
+       if (asprintf(&path, "%s/%s", dir, node) == -1) {
+               free(buf);
+               return ENOMEM;
+       }
 
        ret = xs_write(h, XBT_NULL, path, buf, strlen(buf)+1);
        
@@ -180,10 +182,11 @@ char *get_dom_domid(struct xs_handle *h)
        
        e = xs_directory(h, xth, "/local/domain", &num);
        if (e == NULL)
-               return NULL;
+               goto done;
 
        for (i = 0; (i < num) && (domid == NULL); i++) {
-               asprintf(&path, "/local/domain/%s/name", e[i]);
+               if (asprintf(&path, "/local/domain/%s/name", e[i]) == -1)
+                       break;
                val = xs_read(h, xth, path, &len);
                free(path);
                if (val == NULL)
@@ -191,29 +194,31 @@ char *get_dom_domid(struct xs_handle *h)
                
                if (strcmp(val, DOMNAME) == 0) {
                        /* match! */
-                       asprintf(&path, "/local/domain/%s/domid", e[i]);
+                       if (asprintf(&path, "/local/domain/%s/domid", e[i]) == -1) {
+                               free(val);
+                               break;
+                       }
                        domid = xs_read(h, xth, path, &len);
                        free(path);
                }
                free(val);
        }
+done:
        xs_transaction_end(h, xth, 0);
-       
-       free(e);
+       if (e)
+               free(e);
        return domid;
 }
 
 int convert_dev_name_to_num(char *name) {
-       char *p_sd, *p_hd, *p_xvd, *p_plx, *p, *alpha,*ptr;
+       char *p*ptr;
        int majors[10] = {3,22,33,34,56,57,88,89,90,91};
        int maj,i,ret = 0;
-
-       asprintf(&p_sd,"/dev/sd");
-       asprintf(&p_hd,"/dev/hd");
-       asprintf(&p_xvd,"/dev/xvd");
-       asprintf(&p_plx,"plx");
-       asprintf(&alpha,"abcdefghijklmnop");
-       
+       char *p_sd = "/dev/sd";
+       char *p_hd = "/dev/hd";
+       char *p_xvd = "/dev/xvd";
+       char *p_plx = "plx";
+       char *alpha = "abcdefghijklmnop";
 
        if (strstr(name, p_sd) != NULL) {
                p = name + strlen(p_sd);
@@ -251,12 +256,6 @@ int convert_dev_name_to_num(char *name) {
                ret = BASE_DEV_VAL;
        }
 
-       free(p_sd);
-       free(p_hd);
-       free(p_xvd);
-       free(p_plx);
-       free(alpha);
-
        return ret;
 }
 
index 92c57ec605880bb7e6ce9d261a67fc3a14fb8ca1..d67f4c3abd860a69de9310b964eda6dae6fdb5c6 100644 (file)
@@ -303,7 +303,10 @@ int xs_gather(struct xs_handle *xs, const char *dir, ...)
                void *result = va_arg(ap, void *);
                char *p;
 
-               asprintf(&path, "%s/%s", dir, name);
+               if (asprintf(&path, "%s/%s", dir, name) == -1) {
+                       ret = ENOMEM;
+                       break;
+               }
                p = xs_read(xs, XBT_NULL, path, NULL);
                free(path);
                if (p == NULL) {